/* unix_stream_server.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#define N 64
typedef struct sockaddr SA;
int main(int argc, char *argv[])
{
    int listenfd, connfd;
    char buf[N];
    int on = 1;
    struct sockaddr_un myaddr;
    // 检查命令行参数
    if (argc < 2)
    {
        printf("Usage : %s <ip> <port>\n",argv[0]);
        exit(-1);
    }
    if ((listenfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
    {
        perror("fail to socket");
        exit(-1);
    }
	// 删除套接字文件
	remove(argv[1]);
    bzero(&myaddr, sizeof(myaddr));
	myaddr.sun_family = PF_UNIX;
	strcpy(myaddr.sun_path, argv[1]);

    if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
    {
        perror("fail to bind");
        exit(-1);
    }
	if (listen(listenfd,10) < 0)
	{
		perror("fail to listen");
		exit(-1);
	}
    while(1)
    {
		if((connfd = accept(listenfd, NULL, NULL)) < 0)
		{
			perror("fail to accept");
			exit(-1);
		}
		recv(connfd, buf, N, 0);
		printf(" recv from client : %s\n", buf);
		close(connfd);
    }
    return 0;
}